<?php

$client_obj = new client_obj();
$record_obj = new record_obj();
$user_obj = new user_obj();
$tariff_obj = new tariff_obj();

$filter = array(
    'active' => 0
);


$clients = $client_obj->getAllClients('klient_id, name, bad_payer', $filter, 'name ASC');
$GLOBALS['template_data']['clients'] = $clients;
$GLOBALS['template_data']['today'] = date("d/m/Y");
$GLOBALS['template_data']['prefill']['client'] = $_SESSION['user']['clientFilter'];
$GLOBALS['template_data']['prefill']['dateFrom'] = date("d/m/Y");
$GLOBALS['template_data']['prefill']['dateTo'] = date("d/m/Y");
if (!empty($_SESSION['user']['clientFilter'])) {
    $filter = array(
        'client' => $_SESSION['user']['clientFilter'],
        'userlevel' => USER_TYPE_CLIENT
    );
    $GLOBALS['template_data']['users'] = $user_obj->getUsers("users_id, surname, name, prefered", $filter);
}

if (isset($_REQUEST['selectedClient'])
        && is_numeric($_REQUEST['selectedClient'])
        && !empty($_REQUEST['selectedClient'])
        && isset($_REQUEST['selectedDateFrom'])
        && !empty($_REQUEST['selectedDateFrom'])
        && isset($_REQUEST['selectedDateTo'])
        && !empty($_REQUEST['selectedDateTo'])
) {
    if (strtotime(str_replace('/', '-', $_REQUEST['selectedDateTo'])) >= strtotime(str_replace('/', '-', $_REQUEST['selectedDateFrom']))) {

        $GLOBALS['template_data']['prefill']['client'] = $_REQUEST['selectedClient'];
        $GLOBALS['template_data']['prefill']['dateFrom'] = $_REQUEST['selectedDateFrom'];
        $GLOBALS['template_data']['prefill']['dateTo'] = $_REQUEST['selectedDateTo'];
        if (!empty($_REQUEST['selectedClient'])) {
            $filter = array(
                'client' => $_REQUEST['selectedClient'],
                'userlevel' => USER_TYPE_CLIENT
            );
            $GLOBALS['template_data']['users'] = $user_obj->getUsers("users_id, surname, name, prefered", $filter);
        }
        if (!empty($_REQUEST['selectedUser']))
            $GLOBALS['template_data']['prefill']['user'] = $_REQUEST['selectedUser'];

        $dateFrom = base_obj::convertDateTo($_REQUEST['selectedDateFrom']);
        $dateTo = base_obj::convertDateTo($_REQUEST['selectedDateTo']);
        $records_type = $record_obj->getRecordTypes('Id ASC');
        $filter = array();
        $filter['client'] = $_REQUEST['selectedClient'];
        $filter['date-equal-higher'] = $dateFrom;
        $filter['date-equal-lower'] = $dateTo;

        $output_records = array();
        $output_sums = array();
        $total_sums = array();
        $total_sums['withoutDph'] = 0;
        $total_sums['withDph'] = 0;

        if (empty($_SESSION['protocol_order']))
            $_SESSION['protocol_order'] = array(
                'orderBy' => 'date',
                'order' => 'ASC'
            );


        foreach ($records_type as $key => $type) {
            $filter['type'] = $type['Id'];
            $sk = array(
                1 => 'Vykonané práce',
                3 => 'Doprava',
                4 => 'Príjazd do hodiny',
                8 => 'Dodaný tovar',
            );
            $type['type_of_record'] = $sk[$type['Id']];
            $records = $record_obj->getListOfRecords($filter, $_SESSION['protocol_order']);
            if (!empty($records)) {
                $output_sums[$type['type_of_record']]['withoutDph'] = 0;
                $output_sums[$type['type_of_record']]['withDph'] = 0;
                foreach ($records as $k => $record) {
                    $records[$k]['date_formated'] = base_obj::convertDateTo($record['date'], 'dd/mm/yyyy', 'date');
                    $records[$k]['withoutDph'] = $record['amount'] * $record['unit_price'];
                    $records[$k]['withDph'] = $records[$k]['withoutDph'] * DPH + $records[$k]['withoutDph'];
                    $records[$k]['user_info'] = $user_obj->getUserById($record['user_id'], array('name', 'surname'));
                    if ($type['Id'] == 1)
                        $records[$k]['tariff_info'] = $tariff_obj->getTariffById($record['tariff']);
                    else
                        $records[$k]['tariff_info']['title'] = '';
                    $output_sums[$type['type_of_record']]['withoutDph'] += $records[$k]['withoutDph'];
                    $output_sums[$type['type_of_record']]['withDph'] += $records[$k]['withDph'];
                }
                $output_records[$type['type_of_record']] = $records;
            }
            $total_sums['withoutDph'] += $output_sums[$type['type_of_record']]['withoutDph'];
            $total_sums['withDph'] += $output_sums[$type['type_of_record']]['withDph'];
        }

        $GLOBALS['template_data']['client_info'] = $client_obj->getClient($_REQUEST['selectedClient']);
        if (!empty($GLOBALS['template_data']['client_info']['contactPerson']))
            $GLOBALS['template_data']['contact_info'] = $user_obj->getUserById($GLOBALS['template_data']['client_info']['contactPerson']);
        if (!empty($_REQUEST['selectedUser']))
            $GLOBALS['template_data']['user_info'] = $user_obj->getUserById($_REQUEST['selectedUser']);
        $GLOBALS['template_data']['records'] = $output_records;
        $GLOBALS['template_data']['sums'] = $output_sums;
        $GLOBALS['template_data']['total_sums'] = $total_sums;
    }
}
?>
